package com.xinjie.www.dao.report;

import com.xinjie.www.dao.BaseDao;
import com.xinjie.www.po.Report;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ReportDaoImpl implements ReportDao {
    //进行举报
    @Override
    public int doReport(Connection connection, int postId, int userId) throws SQLException {
        PreparedStatement pstm = null;
        Object[] params = {postId, userId};
        String sql = "insert into report (post_id,user_id) values(?,?)";
        int excute = BaseDao.excute(connection, pstm, sql, params);
        BaseDao.closeResource(null, pstm, null);
        return excute;
    }

    //获得举报列表
    @Override
    public List<Report> getReport(Connection connection, String tieba) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        Object[] params = {tieba};
        List<Report> reports = new ArrayList<>();
        String sql = "select * from report r,post p where r.post_id=p.post_id and p.tieba = ?";
        rs = BaseDao.excute(connection, pstm, rs, sql, params);
        while (rs.next()) {
            Report report = new Report();
            report.setReportId(rs.getInt("report_id"));
            report.setPostId(rs.getInt("post_id"));
            report.setUserId(rs.getInt("user_id"));
            reports.add(report);
        }
        BaseDao.closeResource(null, pstm, rs);
        return reports;
    }
}
